<template>
	<view>
		<view class="padding"></view>
		<view class="content">
			<view class="nav">
				<view class="nav-left">
					<scroll-view scroll-y :style="'height:'+height+'px'">
						<view class="nav-left-item" @click="categoryClickMain(item.id)" :key="index" :class="item.id==categoryActive?'active':''"
						 v-for="(item,index) in categoryList">
							{{item.name}}
						</view>
					</scroll-view>
				</view>
				<view class="nav-right">
					<scroll-view scroll-y :scroll-top="scrollTop" @scroll="scroll" :style="'height:'+height+'px'"
					 scroll-with-animation>
						<view :id="id" class="nav-right-item" v-for="(item,index) in subCategoryList" :key="index" @click="goGoodsList(item.id)">
							<image :src="item.imgUrl" />
							<view>{{item.name}}</view>
						</view>
					</scroll-view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		get,
		post,
		showToast
	} from '../../js/common';
	export default {
		data() {
			return {
				id: '',
				categoryList: {},
				subCategoryList: {},
				height: 0,
				categoryActive: 0,
				scrollTop: 0,
				scrollHeight: 0,
			}
		},
		methods: {
			scroll(e) {
				this.scrollHeight = e.detail.scrollHeight;
			},
			categoryClickMain(id) {
				this.categoryActive = id;
				this.scrollTop = -this.scrollHeight * id;
				const data = {
					'parentId': id
				}
				post('mobileapi/anon/cate/getCategoryById', data).then(res => {
					this.subCategoryList = res.cateList;
				})
			},
			getCategory() {
				post('mobileapi/anon/cate/getParentCategory').then(res => {
					this.categoryList = res.parentCate
					if(this.id){
						this.categoryClickMain(this.id)
					}else{
						this.categoryClickMain(this.categoryList[0].id)
					}
				})
			},
			goGoodsList(id) {
				uni.navigateTo({
					url: '/pages/productList/index?id=' + id
				})
			}
		},
		onLoad: function(option) {
			this.id = option.cateId
			this.getCategory()
			uni.getSystemInfo({
				success: res => {
					this.height = res.screenHeight;
				}
			})
		}
	}
</script>

<style scoped>
	.nav {
		display: flex;
		width: 100%;
	}

	.nav-left {
		width: 24%;
		border-right: solid 1px #E0E0E0;
		background: #FFFFFF;
	}

	.nav-left-item {
		height: 100upx;
		border-bottom: solid 1px #E0E0E0;
		font-size: 30upx;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.nav-right {
		width: 76%;
		padding-top: 10upx;
		padding-left: 10upx;
		background: #ececec;
	}

	.nav-right-item {
		float: left;
		text-align: center;
		padding: 10upx;
		font-size: 28upx;
		background-color: #FFFFFF;
		margin: 10upx;
		border-radius: 6upx;
	}

	.nav-right-item image {
		width: 148upx;
		height: 148upx;
	}

	.active {
		color: #D7000F;
		border-left: 2px solid #D7000F;
	}

	.padding {
		height: var(--status-bar-height);
		width: 100%;
		top: 0;
		position: fixed;
		background-color: #F24544;
	}

	.content {
		padding-top: var(--status-bar-height);
		width: 100%;
	}
</style>
